package day01_IO_JDBC.demo_JDBC_emp_pro.Net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EmpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务端已启动，等待客户端连接...");
        while (true) {
            Socket socket = serverSocket.accept();
            new Thread(new ClientHandler(socket)).start();
        }
    }

    /*
    * 服务端每接收到一个客户端连接，
    * 都会启动一个新线程（ClientHandler），实现多线程并发处理。
    * */
    static class ClientHandler implements Runnable {
        private Socket socket;
        public ClientHandler(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            try {
                OutputStream os = socket.getOutputStream();
                String msg = "欢迎使用人事管理系统！\n";
                os.write(msg.getBytes());
                os.flush();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
